<?php

header("Content-type: application/xhtml+xml");

require_once('../lib/Smarty/Smarty.class.php');
require_once('../lib/People.php');
require_once('../lib/Schedule.php');
require_once('../lib/Util.php');
require_once('../lib/Constants.php');
require_once('../lib/Config.php');

$data   = make_page(array_merge($_GET, $_POST));
$smarty = new Smarty();
$smarty->assign($data);
$smarty->display($data['error_code'] ? 'error.tmpl' : 'edit_hima_flag.tmpl');

function make_page ($params) {
    $data = array(
        'error_code' => 0
    );

    if (!validate_signature(array_merge($_GET, $_POST))) {
        $data['error_code'] = 1;
        return $data;
    }
    if (!isset($params['schedule_id']) || !is_numeric($params['schedule_id'])) {
        $data['error_code'] = 1;
        return $data;
    }
    if (!isset($params['hima_flag']) || !is_numeric($params['hima_flag'])) {
        $data['error_code'] = 1;
        return $data;
    }

    $user     = $params['opensocial_owner_id'];
    $flag     = $params['hima_flag'];
    $schedule = new Schedule($params['schedule_id']);

    if (!$schedule || !$schedule->id || $schedule->member_id != $user) {
        $data['error_code'] = 1;
        return $data;
    }

    $result;
    if ($flag == 3)
        $result = $schedule->delete();
    else {
        $schedule->flag = $flag;
        $result = $schedule->update();
    }

    $data['flag']      = $flag;
    $data['unixstamp'] = date_to_unix($schedule->start_date);
    $data['result']    = $result;
    $data['schedule']  = $schedule;
    $data['date']      = $schedule->start_date;
    $data['url_prefix'] = URL_PREFIX;

    $data['analytics']    = '<img src="' . googleAnalyticsGetImageUrl() . '" />'; // for google analytics

    return $data;
}

?>
